function PreferencesAssistant(appAssistant) {
	Mojo.Log.info('--- BEGIN PreferencesAssistant(), appAssistant: ' + appAssistant);
	
	this.appAssistant = appAssistant;
}

PreferencesAssistant.prototype.setup = function() {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.setup()");
	
	try {
		// refresh_and_hide_button button
		this.refreshAndHideButtonModel = {
		    label : "Refresh and Hide",
			buttonClass: 'affirmative',
		    disabled: false
		}
		this.controller.setupWidget("refresh_and_hide_button", {}, this.refreshAndHideButtonModel);
		Mojo.Event.listen(this.controller.get("refresh_and_hide_button"), Mojo.Event.tap, this.refreshCalendarEvents.bind(this));
		
		// show all day events
		this.toggleattsShowAllDayEvents = {
			trueLabel: 'Yes',
			falseLabel: 'No'
		};
		this.togglemodelShowAllDayEvents = {
	        value: Preferences.showAllDayEvents
		};
		this.controller.setupWidget("togglebuttonShowAllDayEvents", this.toggleattsShowAllDayEvents, this.togglemodelShowAllDayEvents);
	    Mojo.Event.listen(this.controller.get('togglebuttonShowAllDayEvents'), Mojo.Event.propertyChange, this.togglechangeShowAllDayEvents.bindAsEventListener(this));
	
		// show timed events
		this.toggleattsShowTimedEvents = {
			trueLabel: 'Yes',
			falseLabel: 'No'
		};
		this.togglemodelShowTimedEvents = {
	        value: Preferences.showTimedEvents
		};
		this.controller.setupWidget("togglebuttonShowTimedEvents", this.toggleattsShowTimedEvents, this.togglemodelShowTimedEvents);
	    Mojo.Event.listen(this.controller.get('togglebuttonShowTimedEvents'), Mojo.Event.propertyChange, this.togglechangeShowTimedEvents.bindAsEventListener(this));

		// show passed events
		this.toggleattsShowPassedEvents = {
			trueLabel: 'Yes',
			falseLabel: 'No'
		};
		this.togglemodelShowPassedEvents = {
	        value: Preferences.showPassedEvents
		};
		this.controller.setupWidget("togglebuttonShowPassedEvents", this.toggleattsShowPassedEvents, this.togglemodelShowPassedEvents);
	    Mojo.Event.listen(this.controller.get('togglebuttonShowPassedEvents'), Mojo.Event.propertyChange, this.togglechangeShowPassedEvents.bindAsEventListener(this));

		// refresh interval
		this.refreshMinutesAttributes = {
			label:	'Minutes',
			min: 0,
			max: 59,
			modelProperty:	'value'
		};
		this.refreshMinutesModel = {
			value : Preferences.refreshMinutes
		}
		this.controller.setupWidget('refresh_minutes', this.refreshMinutesAttributes, this.refreshMinutesModel);
		this.controller.listen(this.controller.get("refresh_minutes"), Mojo.Event.propertyChange, this.handleRefreshMinutes.bind(this));
		
		this.refreshHoursAttributes = {
			label:	'Hours',
			min: 0,
			max: 24,
			modelProperty:	'value'
		};
		this.refreshHoursModel = {
			value : Preferences.refreshHours
		}
		this.controller.setupWidget('refresh_hours', this.refreshHoursAttributes, this.refreshHoursModel);
		this.controller.listen(this.controller.get("refresh_hours"), Mojo.Event.propertyChange, this.handleRefreshHours.bind(this));
		
		// upcoming days to show
		this.upcomingDaysAttributes = {
			label:	'Days To Show',
			min: 0,
			max: 28,
			modelProperty:	'value'
		};
		this.upcomingDaysModel = {
			value : Preferences.upcomingDays
		}
		this.controller.setupWidget('upcoming_days', this.upcomingDaysAttributes, this.upcomingDaysModel);
		this.controller.listen(this.controller.get("upcoming_days"), Mojo.Event.propertyChange, this.handleUpcomingDays.bind(this));
		
		// maximum events to show
		this.maximumEventsAttributes = {
			label:	'Maximum Dates To Show',
			min: 0,
			max: 10,
			modelProperty:	'value'
		};
		this.maximumEventsModel = {
			value : Preferences.maximumEvents
		}
		this.controller.setupWidget('maximum_events', this.maximumEventsAttributes, this.maximumEventsModel);
		this.controller.listen(this.controller.get("maximum_events"), Mojo.Event.propertyChange, this.handleMaximumEvents.bind(this));
		
		// calendars to show
		this.calendarsToShowAttributes = {
			itemTemplate: 'preferences/prefsRowTemplate',
			listTemplate: 'preferences/prefsListTemplate',
			swipeToDelete: false,
			renderLimit: 30,
			reorderable: false
		};
		this.calendarsToShowModel = {
			items: Preferences.calendars
		};
		this.controller.setupWidget('calendarList', this.calendarsToShowAttributes, this.calendarsToShowModel);
		this.controller.listen('calendarList', Mojo.Event.propertyChange, this.calendarsToShowChange.bindAsEventListener(this));
		
		// checkbox for show entries
		this.calendarsToShowCheckBoxModel = {
			modelProperty: 'display'
		};
		this.controller.setupWidget('listCheckBox', this.calendarsToShowCheckBoxModel);
		
		// create menu    
	    this.appMenuAttr = {omitDefaultItems: true};
		this.appMenuModel = {
	            visible: true,
	            items: [
					Mojo.Menu.editItem,
	                { label: 'About...', command: 'do-about' }
	            ]
	        };
		this.controller.setupWidget(Mojo.Menu.appMenu, this.appMenuAttr, this.appMenuModel);
	}
		
	catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.setup", e);
	}	
}

PreferencesAssistant.prototype.calendarsToShowChange = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.calendarsToShowChange()");
	
	try {
		Mojo.Log.info("--- PreferencesAssistant.calendarsToShowChange(): event %j", event);
		Mojo.Log.info("--- PreferencesAssistant.calendarsToShowChange(): id: " + event.model.id + ", display: " + event.model.display);
		
		this.refreshCalendarsToHide();
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
		
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.calendarsToShowChange", e);
	}
}

PreferencesAssistant.prototype.activate = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.activate()");
}

PreferencesAssistant.prototype.deactivate = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.deactivate()");
}

PreferencesAssistant.prototype.cleanup = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.cleanup()");
	
	try {
		this.controller.stopListening(this.controller.get('refresh_and_hide_button'), Mojo.Event.tap, this.refreshCalendarEvents);
		this.controller.stopListening(this.controller.get('togglebuttonShowAllDayEvents'), Mojo.Event.propertyChange, this.togglechangeShowAllDayEvents);
		this.controller.stopListening(this.controller.get('togglebuttonShowTimedEvents'), Mojo.Event.propertyChange, this.togglechangeShowTimedEvents);
		this.controller.stopListening(this.controller.get('togglebuttonShowPassedEvents'), Mojo.Event.propertyChange, this.togglechangeShowPassedEvents);
		this.controller.stopListening(this.controller.get('refresh_minutes'), Mojo.Event.propertyChange, this.handleRefreshMinutes);
		this.controller.stopListening(this.controller.get('refresh_hours'), Mojo.Event.propertyChange, this.handleRefreshHours);
		this.controller.stopListening(this.controller.get('upcoming_days'), Mojo.Event.propertyChange, this.handleUpcomingDays);
		this.controller.stopListening(this.controller.get('maximum_events'), Mojo.Event.propertyChange, this.handleMaximumEvents);
		this.controller.stopListening(this.controller.get('calendarList'), Mojo.Event.propertyChange, this.calendarsToShowChange);
		
		this.controller.stopListening(this.controller.get('calendarList'), Mojo.Event.propertyChange, this.calendarsToShowChange);
		
		Mojo.Controller.getAppController().assistant.setRefreshTimerInterval({});
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.cleanup", e);
	}
}

PreferencesAssistant.prototype.updatePreferences = function() {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.updatePreferences()");
	
	try {
		this.togglemodelShowAllDayEvents.value = Preferences.showAllDayEvents;
		this.togglemodelShowTimedEvents.value = Preferences.showTimedEvents;
		this.togglemodelShowPassedEvents.value = Preferences.showPassedEvents;
		this.upcomingDaysModel.value = Preferences.upcomingDays;
		this.maximumEventsModel.value = Preferences.maximumEvents;
		this.refreshHoursModel.value = Preferences.refreshHours;
		this.refreshMinutesModel.value = Preferences.refreshMinutes;
		this.calendarsToShowModel.item = Preferences.calendars;
		
		this.controller.modelChanged(this.togglemodelShowAllDayEvents, this);
		this.controller.modelChanged(this.togglemodelShowTimedEvents, this);
		this.controller.modelChanged(this.togglemodelShowPassedEvents, this);
		this.controller.modelChanged(this.upcomingDaysModel, this);
		this.controller.modelChanged(this.maximumEventsModel, this);
		this.controller.modelChanged(this.refreshHoursModel, this);
		this.controller.modelChanged(this.refreshMinutesModel, this);
		this.controller.modelChanged(this.calendarsToShowModel, this);
		
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.updatePreferences", e);
	}	
}

PreferencesAssistant.prototype.refreshCalendarEvents = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.refreshCalendarEvents()");
	
	try {
		
		// update calendarsToHide array
		Mojo.Log.info("OK!");
		Mojo.Log.info('Number Calendars is: ' + Preferences.calendars.length);

		Mojo.Log.info("OK!");
		
		
		// call refresh in AppAssistant		
		Mojo.Controller.getAppController().assistant.refreshCalendarEvents({closePreferences: true});
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.refreshCalendarEvents", e);
	}	
}

PreferencesAssistant.prototype.refreshCalendarsToHide = function() {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.refreshCalendarsToHide()");
	
	try {
		Preferences.calendarsToHide = [];
		for (i = 0; i < Preferences.calendars.length; i++) {
			if (Preferences.calendars[i].display == false) {
				Preferences.calendarsToHide.push(Preferences.calendars[i].id);
			}
		}
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.refreshCalendarsToHide", e);
	}	
}

PreferencesAssistant.prototype.togglechangeShowAllDayEvents = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.togglechangeShowAllDayEvents()");
	
	try {
		Preferences.showAllDayEvents = this.togglemodelShowAllDayEvents.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.togglechangeShowAllDayEvents", e);
	}	
}

PreferencesAssistant.prototype.togglechangeShowTimedEvents = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.togglechangeShowTimedEvents()");
	
	try {
		Preferences.showTimedEvents = this.togglemodelShowTimedEvents.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.togglechangeShowTimedEvents", e);
	}	
}

PreferencesAssistant.prototype.togglechangeShowPassedEvents = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.togglechangeShowPassedEvents()");
	
	try {
		Preferences.showPassedEvents = this.togglemodelShowPassedEvents.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.togglechangeShowPassedEvents", e);
	}	
}

PreferencesAssistant.prototype.handleUpcomingDays = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.handleUpcomingDays()");
	
	try {
		Preferences.upcomingDays = this.upcomingDaysModel.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.handleUpcomingDays", e);
	}	
}

PreferencesAssistant.prototype.handleMaximumEvents = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.handleMaximumEvents()");
	
	try {
		Preferences.maximumEvents = this.maximumEventsModel.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.handleMaximumEvents", e);
	}	
}

PreferencesAssistant.prototype.handleRefreshMinutes = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.handleRefreshMinutes()");
	
	try {
		Preferences.refreshMinutes = this.refreshMinutesModel.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
		
		if (this.refreshMinutesModel.value == 0 && this.refreshHoursModel.value == 0) {
			this.refreshHoursModel.value = 1;
			this.controller.modelChanged(this.refreshHoursModel, this);
			this.handleRefreshHours(this);
		}
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.handleRefreshMinutes", e);
	}	
}

PreferencesAssistant.prototype.handleRefreshHours = function(event) {
	Mojo.Log.info("--- BEGIN PreferencesAssistant.handleRefreshHours()");
	
	try {
		Preferences.refreshHours = this.refreshHoursModel.value;
		this.appAssistant.depot.simpleAdd("preferences", Preferences);
		
		if (this.refreshMinutesModel.value == 0 && this.refreshHoursModel.value == 0) {
			this.refreshMinutesModel.value = 1;
			this.controller.modelChanged(this.refreshMinutesModel, this);
			this.handleRefreshMinutes(this);
		}
	} catch (e) {
		Mojo.Log.error("--- ERROR: PreferencesAssistant.handleRefreshHours", e);
	}	
}

PreferencesAssistant.prototype.handleCommand = function(event) {
	if (event.type == Mojo.Event.command) {
		this.cmd = event.command;

		switch(this.cmd) {
			
            case 'do-about':
                this.controller.showAlertDialog({
                    onChoose: function() {},
                    title: $L("UpDates for webOS " + Mojo.Controller.appInfo.version),
                    message: $L("Copyright 2010, " + Mojo.Controller.appInfo.vendor + "  " + Mojo.Controller.appInfo.vendorurl),

                    choices:[
                        {label:$L("OK"), type:'affirmative'}
                    ]
                });
                break;
            
			default:
				break;

		} //switch
	} //if
}
